package com.example.util.common;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2020/9/23 9:54
 */
@Slf4j
public class CustromStringUtils {

    /**
     *
     * @param object
     * @param key
     * @return value or ""
     */
    public String getStrByKey(JSONObject object,String key){
        return Optional.ofNullable(getStr(object,key)).orElse("");
    }

    /**
     *
     * @param object
     * @param key
     * @param defaultVal
     * @return value or defaultVal
     */
    public String getStrByKeyOrDefault(JSONObject object,String key,String defaultVal){
        return Optional.ofNullable(getStr(object,key)).orElse(defaultVal);
    }

    public String getStr(JSONObject object,String key){
        return object.getString(key);
    }


}
